#include "epub_book.h"

#define BOOK_XSIZE  480  
#define BOOK_YSIZE  272

#define BOOK_LINES 17
#define BOOK_LINES_NUM 61

typedef struct epub_boc{
    uint16_t iX;
    uint16_t iY;
    uint16_t width;
    uint16_t hight;
    
    char CN_width;
    char EN_width;
    char line_hight;
    
    char file_name[100]; 
    char *file_data;
    
    char lines;
    char lines_num;
    char book[BOOK_LINES][BOOK_LINES_NUM];
    
}epub_book_t;

epub_book_t my_epub_book;

void my_epub_book_init(){
    my_epub_book.iX = 0;
    my_epub_book.iY = 0;
    my_epub_book.width = BOOK_XSIZE;
    my_epub_book.hight = BOOK_YSIZE;
    my_epub_book.CN_width = 16;
    my_epub_book.EN_width = 8;
    my_epub_book.line_hight = 16;
    my_epub_book.lines = 0;
    my_epub_book.lines_num = 0;
}

void draw_book_page(){
    char key;
    if(my_epub_book.lines >= BOOK_LINES){
        draw_book_show();       
        while(1){
            key = key_scan();
            if(key){
                break;
            }
        }
    }
}

void draw_book(char *str){
    static char flag = 0;
    switch(flag){
        case 0:
//            my_epub_book.lines = 0;
//            my_epub_book.lines_num = 0;
            flag = 1;
        //break;
        case 1:
            while(*str){
                if(*str <= 0x7f){
                    if(my_epub_book.lines_num >= (BOOK_LINES_NUM-1)){
                        my_epub_book.book[my_epub_book.lines][my_epub_book.lines_num] = 0;
                        my_epub_book.lines_num = 0;
                        my_epub_book.lines++;
                        draw_book_page();
                    }
                    if(*str == '\n'){
                        my_epub_book.book[my_epub_book.lines][my_epub_book.lines_num] = 0;
                        my_epub_book.lines_num = 0;
                        my_epub_book.lines++;
                    }
                    my_epub_book.book[my_epub_book.lines][my_epub_book.lines_num] = *str;
                    str++;
                    my_epub_book.lines_num++;
                }else{
                    if(my_epub_book.lines_num >= (BOOK_LINES_NUM-2)){
                        my_epub_book.book[my_epub_book.lines][my_epub_book.lines_num] = 0;
                        my_epub_book.lines_num = 0;
                        my_epub_book.lines++;
                        draw_book_page();
                    }
                    my_epub_book.book[my_epub_book.lines][my_epub_book.lines_num] = *str;
                    my_epub_book.book[my_epub_book.lines][my_epub_book.lines_num + 1] = *(str+1);
                    str+=2;
                    my_epub_book.lines_num+=2;
                }
            
            }
        break;
    }
}

void draw_book_show(){
    int i;
     GUI_Clear_RGB565(RGB565_BLACK);
    draw_string_for_sd_start(); 
    for(i = 0; i < my_epub_book.lines; i++ ){
        draw_string_for_sd(0  ,0 + 16 * i, 16,&my_epub_book.book[i][0],RGB565_GREEN);
    }
    draw_string_for_sd_end();
    
    my_epub_book.lines_num = 0;
    my_epub_book.lines=0;
}

//查找<
int find_right_V(const char *str){
    int num = 0;
    while(1){
        if(str[num] == '<'){
            break;
        }
        num++;
        if(num > 10000){
            return 0;
        }
    }
    return num;
}

int find_left_V(const char *str){
    int num = 0;
    while(1){
        if(str[num] == '>'){
            break;
        }
        num++;
        if(num > 10000){
            return 0;
        }
    }
    return num;
}

void utf_8_to_gbk_str(unsigned char *str){
    int num = 0;
    int gbk_num = 0;
    uint16_t data_temp;
    //char data[100];
    unsigned char *pt = str;
    while( pt[num]){
        if(pt[num] <= 0x7f){
            pt[gbk_num] = pt[num];
            num++;
            gbk_num++;
        }else{
            if(pt[num] <= 0xD1){
                pt[gbk_num] = '-';
                pt[gbk_num + 1] = '-';
                num+=2;
                gbk_num+=2;
            }else{
                data_temp = utf8_to_gbk(pt[num],pt[num+1],pt[num+2]); 
                if(data_temp == 0){
                    pt[gbk_num] = '.';
                    pt[gbk_num + 1] = '.';
                }else{
                    pt[gbk_num] = data_temp / 256;
                    pt[gbk_num + 1] = data_temp % 256;
                }
                num+=3;
                gbk_num+=2;
            }
        }
    }
    pt[gbk_num] = 0;
    
}

void show_h1(const char *str){
    char data[100];
    int num;
    str += 4;
    num = find_right_V( str);
    memcpy(data,str,num);
    data[num] = '\n';
    data[num + 1] = 0;
    utf_8_to_gbk_str(data);
    
    draw_book("        "); 
    draw_book(data);   
    //draw_string_for_sd(50 + 120,0 , 16,data,RGB565_GREEN);
    

}
void show_h3(const char *str){
    char data[100];
    int num;
    str += 4;
    num = find_right_V( str);
    memcpy(data,str,num);
    data[num] = '\n';
    data[num + 1] = 0;
    utf_8_to_gbk_str(data);
    
    draw_book("        "); 
    draw_book(data);   
    //draw_string_for_sd(50 + 120,0 , 16,data,RGB565_GREEN);
    

}

static unsigned char VpV_data[2048];
void show_VpV(const char *str ){
     
    int num;
    str += 3;
    num = find_right_V( str);
    
    memcpy(VpV_data,str,num);
    VpV_data[num] = '\n';
    VpV_data[num + 1] = 0;
    utf_8_to_gbk_str(VpV_data);
    draw_book(VpV_data);    //===================
}


void show_Vp(const char *str ){
    int num = 0;
    //char data[100];
    
    str += 2;
    while(1){
       num = find_left_V( str);
        str += (num+1);
       if(str[0] == '<'){
       
       }else{
            break;
       }     
    }
    
    num = find_right_V( str);
    
    memcpy(VpV_data,str,num);
    //data[num] = 0;
    VpV_data[num] = '\n';
    VpV_data[num + 1] = 0;
    utf_8_to_gbk_str(VpV_data);
    
    draw_book(VpV_data); 
    //draw_string_for_sd(50  ,16 + 16 * y_num, 16,data,RGB565_GREEN);
     
}


int epub_show_one_file_data(const char * file_data){
    char *body;
    char *h1;
    char *h3;
    static char *VpV;
    char *Vp;
    
    int vp_num = 0;
    
   
    
    body = strstr(file_data, "body");
    
    
    h1 = strstr(body, "<h1>");
    if(h1 != NULL){
        show_h1(h1);            
    }
    h3 = strstr(body, "<h3>");
    if(h3 != NULL){
        show_h3(h3);            
    }

    body = strstr(file_data, "body");
    VpV  = strstr(body, "<p>");
    if(VpV != NULL){
        show_VpV(VpV );
        while(1){
        //for(int i = 0; i < 10;i++){
            VpV+=3;
            VpV  = strstr(VpV, "<p>");
            if(VpV != NULL){
                show_VpV(VpV );
            }else{
                break;
            }
        }
    }
    else{
    
        Vp = strstr(body, "<p");
        if(Vp != NULL){
            show_Vp(Vp );        
        } 
         
        while(1){
             Vp+=2;
            Vp = strstr(Vp, "<p");
            if(Vp != NULL){
                show_Vp(Vp );  
                vp_num++;    
            } else{
                break;
            }
        }
    }
    
    sysprintf("epub_show_one_file_data\n");
    
   draw_book_show();    
}

void copy_file_data(void* data,int num){
     my_epub_book.file_data = NULL;
    my_epub_book.file_data = malloc(num+1);
    memcpy(my_epub_book.file_data,data,num);
    my_epub_book.file_data[num] = 0;
 
    //sysprintf("file_data\n%s\n",my_epub_book.file_data);
    
}
void free_file_data(){
    if(my_epub_book.file_data != NULL){
        free(my_epub_book.file_data);
        my_epub_book.file_data = NULL;
        sysprintf("free file_data\n================\n" );
    }
}

void epub_show_book(const char *epub_path){
    //char str[100]; 
    static int book_file_num = 3;
    int res;
    char key;
    static char flag = 0;
    while(1){
        switch(flag){
            case 0:
                flag = 1;
            //break;
            case 1://读取文件名
                sysprintf("book_file_num======\n%d\n",book_file_num);
                res = extract_opf_from_epub_one_filename_OPF(epub_path,book_file_num,my_epub_book.file_name );
                if(res == 0){
                    flag = 2;
                    sysprintf("my_epub_book.file_name======\n%s\n",my_epub_book.file_name);
                }else{
                    sysprintf("my_epub_book.file_name err======\n%s\n",my_epub_book.file_name);
                    return;
                }
                epub_open_one_file_read_data( epub_path,my_epub_book.file_name);
            break;
            case 2://显示文件
                epub_show_one_file_data( my_epub_book.file_data);//==========================
                sysprintf("file_data\n%s\n",my_epub_book.file_data);
                free_file_data();
                //sysprintf("my_epub_book.file_name======\n%s\n",my_epub_book.file_name);
                //epub_open_one_file_read_data( epub_path,my_epub_book.file_name);
                flag = 3;
            break;
            case 3://
                key = key_scan();
                if(key){
                    book_file_num++;
                    sysprintf("book_file_num======\n%d\n",book_file_num);
                    flag = 0;
                }
            break;
        
        }
    }

}

//==============================
#define AUTO_KEY_DELAY 50
char auto_key(){
    static char flag = 0;
    static uint16_t time = 0;
    switch(flag){
        case 0:
            time = 0;
            flag = 1;
        //break;
        case 1:
            delay_ms(10);
            time++;
            if(time > AUTO_KEY_DELAY){
                flag = 0;
                return 1;
            }
        break;
    
    }
    return 0;
}
char get_key_num(){
    return auto_key();
}
char key_scan(){
    char key;
    key = get_key_num();
    return key;

}











